﻿// Copyright © 2012 X-treem Software Limited
// Written by Jason Payne
// Licence: https://wpfgooglesearch.codeplex.com/license

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Xtreem.GoogleSearch.Core
{
    public class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public bool SetValue<T>(ref T property, T value, Expression<Func<T>> propertyDelegate, bool forceChange = false)
        {
            if (Equals(property, value) && !forceChange)
            {
                return false;
            }

            property = value;
            RaisePropertyChanged(propertyDelegate);
            return true;
        }

        private void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(((MemberExpression)propertyExpression.Body).Member.Name));
            }
        }
    }
}
